#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_platform.h"

#include "zj_logger.h"
#include "zj_string.hpp"

#include "AppState.hpp"
#include "PageManager.hpp"
#include "WelcomePage.hpp"

#define LogTag "main"


int main(int args, char *argv[])
{
    AppState app_state;
    memset(&app_state, 0, sizeof(app_state));

    app_state.system_name = SDL_GetPlatform();

#if defined(snake_windows)
    app_state.path = "./";
    app_state.res_path = "./assets/";
#else
    app_state.path = zj_string(SDL_AndroidGetExternalStoragePath()) + "/";
    app_state.res_path = "";
#endif

    app_state.save_path = app_state.path;

    zj_logger_init(app_state.path.c_str(), 0);

    // SDL_ShowSimpleMessageBox(0, SDL_GetPlatform(), "Emmmmmm", nullptr);

    // zj_logger_open_debug();
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
        zj_string e = SDL_GetError();
        zj_logger_e(LogTag, "%s", e.c_str());
        SDL_ShowSimpleMessageBox(0, "错误", e.c_str(), nullptr);
        return -1;
    }
    if (!(IMG_Init(IMG_INIT_JPG|IMG_INIT_PNG) & (IMG_INIT_JPG|IMG_INIT_PNG))) {
        zj_string e = IMG_GetError();
        zj_logger_e(LogTag, "%s", e.c_str());
        SDL_ShowSimpleMessageBox(0, "错误", e.c_str(), nullptr);
        return -1;
    }
    if (TTF_Init() == -1) {
        zj_string e = TTF_GetError();
        zj_logger_e(LogTag, "%s", e.c_str());
        SDL_ShowSimpleMessageBox(0, "错误", e.c_str(), nullptr);
        return -1;
    }

    SDL_GetDisplayMode(0, 0, &app_state.display_mode);    //屏幕大小

    Uint32 window_flags = 0;
    if (app_state.system_name == zj_string("Android")) {
        window_flags = SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
    } else {
        window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
    }
    app_state.window = SDL_CreateWindow("SDL2_app_test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 600, window_flags);
    if (app_state.window == nullptr) {
        zj_string e = SDL_GetError();
        zj_logger_e(LogTag, "%s", e.c_str());
        SDL_ShowSimpleMessageBox(0, "错误", e.c_str(), nullptr);
        return -1;
    }
    app_state.renderer = SDL_CreateRenderer(app_state.window, -1, SDL_RENDERER_ACCELERATED);
    if (app_state.renderer == nullptr) {
        zj_string e = SDL_GetError();
        zj_logger_e(LogTag, "%s", e.c_str());
        SDL_ShowSimpleMessageBox(0, "错误", e.c_str(), nullptr);
        return -1;
    }
    SDL_SetRenderDrawBlendMode(app_state.renderer, SDL_BLENDMODE_BLEND);

    zj_string font_path;
    if (app_state.system_name == zj_string("Android")) {
        font_path = /*app_state.path + "/assets/small-font.ttf";*/app_state.res_path + "small-font.ttf";
    } else {
        font_path = app_state.res_path + "/small-font.ttf";
    }
    app_state.font = TTF_OpenFont(font_path.c_str(), 100);  // 100 已测定
    if (app_state.font == nullptr) {
        zj_string e = zj_string("字体加载失败: ") + zj_string(TTF_GetError());
        zj_logger_e(LogTag, "%s", e.c_str());
        SDL_ShowSimpleMessageBox(0, "错误", e.c_str(), app_state.window);
        return -1;
    }

    app_state.page_manager = new PageManager(&app_state);
    app_state.page_manager->first_page(new WelcomePage());

    while (true) {
        app_state.page_manager->do_step();
        if (app_state.page_manager->ready_to_exit()) {
            zj_logger_d("main", "ready_to_exit()");
            break;
        }
        app_state.page_manager->do_draw();
    }

    delete app_state.page_manager;
    app_state.page_manager = 0;

    TTF_CloseFont(app_state.font);
    SDL_DestroyRenderer(app_state.renderer);
    SDL_DestroyWindow(app_state.window);

    TTF_Quit();
    IMG_Quit();
    SDL_Quit();
    // SDL_ShowSimpleMessageBox(0, "退出", "", 0);
    return 0;
}
